#include <bits/stdc++.h>
// 2025/06/09
// tag: 
// Author: Forever
using namespace std;
using int64 = int64_t;

#define int long long

const int N = 100001, MOD = 1000000007;
int n, k, f[N][4], color[N];
vector<int> edge[N];

void dfs(int u, int fa){
    f[u][1] = 1, f[u][2] = 1, f[u][3] = 1;
    if (color[u])
        f[u][1] = f[u][2] = f[u][3] = 0, f[u][color[u]] = 1;
    for (int v : edge[u]){
        if (v == fa) continue;
        dfs(v, u);
        if (color[u]){
            f[u][color[u]] *= ((1 != color[u]) * f[v][1] + (2 != color[u]) * f[v][2] + (3 != color[u]) * f[v][3]) % MOD;
            f[u][color[u]] %= MOD;
        }
        else{
            f[u][1] *= f[v][2] + f[v][3];
            f[u][2] *= f[v][1] + f[v][3];
            f[u][3] *= f[v][1] + f[v][2];
            f[u][1] %= MOD, f[u][2] %= MOD, f[u][3] %= MOD;
        }
    }
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> k;
    for (int i = 1; i < n; i++){
        int u, v;
        cin >> u >> v;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }
    for (int i = 1; i <= k; i++){
        int b, c;
        cin >> b >> c;
        color[b] = c;
    }
    dfs(1, 0);
    cout << (f[1][1] + f[1][2] + f[1][3]) % MOD << endl;
    return 0;
}